package com.hui.xiao.xiaohui.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher;

import com.hui.xiao.xiaohui.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/21.
 */
public class ImageSwitcherTest extends Activity {
    int[] imageIds = new int[]{
            R.drawable.bkch, R.drawable.aboc_s, R.drawable.aboc,
            R.drawable.bkch, R.drawable.aboc_s, R.drawable.aboc,
            R.drawable.bkch, R.drawable.aboc_s, R.drawable.aboc,
            R.drawable.bkch, R.drawable.aboc_s, R.drawable.aboc
    };
    ImageSwitcher switcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_switcher);
        List<Map<String, Object>> items = new ArrayList<>();
        for (int i = 0; i < imageIds.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("image", imageIds[i]);
            items.add(item);
        }
        switcher = (ImageSwitcher) findViewById(R.id.image_switch);
        switcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(ImageSwitcherTest.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                return imageView;
            }
        });
        SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});
        GridView grid = (GridView) findViewById(R.id.grid01);
        grid.setAdapter(adapter);
        grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switcher.setImageResource(imageIds[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switcher.setImageResource(imageIds[position]);
            }
        });
    }
}
